﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp9
{
    /// <summary>
    /// TodoItemListingView.xaml 的交互逻辑
    /// </summary>
    public partial class TodoItemListingView : UserControl
    {
        public ICommand TodoItemDropCommand
        {
            get { return (ICommand)GetValue(TodoItemDropCommandProperty); }
            set { SetValue(TodoItemDropCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TodoItemDropCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TodoItemDropCommandProperty =
            DependencyProperty.Register("TodoItemDropCommand", typeof(ICommand), typeof(TodoItemListingView), new PropertyMetadata(null));


        public ICommand TodoItemRemoveCommand
        {
            get { return (ICommand)GetValue(TodoItemRemoveCommandProperty); }
            set { SetValue(TodoItemRemoveCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TodoItemRemoveCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TodoItemRemoveCommandProperty =
            DependencyProperty.Register("TodoItemRemoveCommand", typeof(ICommand), typeof(TodoItemListingView), new PropertyMetadata(null));


        public TodoItemListingView()
        {
            InitializeComponent();
        }

        private void TextBlock_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && 
                sender is FrameworkElement frameworkElement)
            {
                TodoItemViewModel item = frameworkElement.DataContext as TodoItemViewModel;
                DragDropEffects dragDropResult = DragDrop.DoDragDrop(frameworkElement, new DataObject(DataFormats.Serializable, item), DragDropEffects.Move);
                if(dragDropResult == DragDropEffects.None)
                {
                    if (TodoItemDropCommand != null)
                        TodoItemDropCommand.Execute(item);
                }
            }
        }

        private void ListView_Drop(object sender, DragEventArgs e)
        {
            if (TodoItemDropCommand != null)
                TodoItemDropCommand.Execute(e.Data.GetData(DataFormats.Serializable));
        }

        private void ListView_DragLeave(object sender, DragEventArgs e)
        {
            HitTestResult result = VisualTreeHelper.HitTest(lv, e.GetPosition(lv));
            if (result == null)
            {
                if (TodoItemRemoveCommand != null)
                    TodoItemRemoveCommand.Execute(e.Data.GetData(DataFormats.Serializable));
            }
        }
    }
}
